#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
const std::string gpipeFile = "./fifo";
const mode_t pri = 0600;

const int defualtFd = -1;
const int gsize = 1024;
const int gForRead = O_RDONLY;
const int gForWrite = O_WRONLY;

class Init
{
private:
public:
    Init();
    ~Init();
};

Init::Init()
{
    umask(0);
    int n = ::mkfifo(gpipeFile.c_str(), pri);
    if (n < 0)
    {
        std::cerr << "mkfifo error" << std::endl;
        return;
    }
    std::cout << "mkfifo success" << std::endl;
    // sleep(5);
}

Init::~Init()
{
    int n = ::unlink(gpipeFile.c_str());
    if (n < 0)
    {
        std::cerr << "unlink error" << std::endl;
        return;
    }
    std::cout << "unlink success" << std::endl;
}

// zdl:: 现在将这个变量直接定义在这里就可在程序退出的是后直接将这个变量清楚掉！！
Init inti;

int openPipe(int flag)
{
    int _fd = ::open(gpipeFile.c_str(), flag);
    if (_fd < 0)
    {
        std::cerr << "open error" << std::endl;
        return _fd;
    }

    return _fd;
}

void closePipeHelper(int fd)
{
    if (fd >= 0)
        ::close(fd);
}